﻿using System;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;
using System.Xml;
using System.IO;

using DevExpress.XtraNavBar;
using Medicalsystem.MCP.FormDesigner.Interface;

namespace Medicalsystem.MCP.FormDesigner.Ctl
{
    public partial class ctl_ControlTree : DevExpress.XtraEditors.XtraUserControl
    {
        public ctl_ControlTree()
        {
            InitializeComponent();
        }

        public delegate void ClickHandler(string controlName, string assName);

        /// <summary>
        /// 点击控件节点触发
        /// </summary>
        public event ClickHandler NodeMouseClickEvent;



        private void OnNodeMouseClickEvent(string controlName, string assName)
        {
            if (NodeMouseClickEvent != null)
            {
                NodeMouseClickEvent(controlName, assName);
            }
        }

        private void navBarControl1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            try
            {
                if (e.Link.Item.Name == "Arrow")
                {
                    OnNodeMouseClickEvent(null, null);
                }
                else
                {
                    OnNodeMouseClickEvent(e.Link.Item.Name, e.Link.Item.Tag.ToString());
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }
        /// <summary>
        /// 动态加载控件树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctl_ControlTree_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                try
                {
                    gNormal.Visible = false;
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CareControlConfig.xml");
                    XmlDocument document = new XmlDocument();
                    document.Load(path);
                    XmlNodeList nodeList = document.GetElementsByTagName("Groups");
                    foreach (XmlNode node in nodeList)
                    {
                        XmlNode nodeName = node.SelectSingleNode("GroupName");
                        NavBarGroup group = new NavBarGroup();
                        group.Caption = nodeName.InnerText;

                        NavBarItem bItem = new NavBarItem();
                        bItem.Name = "Arrow";
                        bItem.SmallImage = Arrow.SmallImage;
                        bItem.Caption = "指 针";

                        group.ItemLinks.Add(Arrow);

                        XmlNodeList childNodeList = node.SelectNodes("Control");
                        foreach (XmlNode childNode in childNodeList)
                        {
                            NavBarItem nItem = new NavBarItem();
                            nItem.Caption = childNode.SelectSingleNode("ControlName").InnerText;
                            string assStr = childNode.SelectSingleNode("ControlAssembly").InnerText;
                            string clsName = childNode.SelectSingleNode("ControlClassName").InnerText;
                            ICtrlBase ctl = (ICtrlBase)CacheManager.Instance.GetObjFrom(assStr, clsName);
                            nItem.SmallImage = ctl.IcoForToolBox;
                            nItem.Name = clsName;
                            nItem.Tag = assStr;
                            group.ItemLinks.Add(nItem);
                        }
                        MynavBarControl.Groups.Add(group);

                    }
                }
                catch 
                {

                }

            }

        }
    }
}